<!DOCTYPE html>
<html>


<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <style>
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .form-input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .form-checkbox {
            margin-right: 10px;
        }

        .form-button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2>医生填写病历</h2>
        <form id="medical-form">
            <div class="form-group">
                <label class="form-label" for="medical-number">病历编号:</label>
                <input class="form-input" type="text" id="medical-number" required>
            </div>
            <div class="form-group">
                <label class="form-label" for="visit-time">就诊时间:</label>
                <input class="form-input" type="datetime-local" id="visit-time" value="2022-01-09T12:00" required>
            </div>
            <div class="form-group">
                <label class="form-label" for="date_of_birth">出生日期:</label>
                <input class="form-input" type="datetime-local" id="date_of_birth" value="2022-01-09T12:00" required>
            </div>
            <div class="form-group">
                <label class="form-label" for="department">科室:</label>
                <input class="form-input" type="text" id="department" required>
            </div>
            <div class="form-group">
                <label class="form-label" for="patient-id">病人编号:</label>
                <input class="form-input" type="text" id="patient-id" rows="4" required>
            </div>
            <div class="form-group">
                <label class="form-label" for="patient-name">病人姓名:</label>
                <input class="form-input" type="text" id="patient-name" rows="4" required>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="form-label" for="patient-gender">病人性别:</label>-->
<!--                <input class="form-input" type="text" id="gender" rows="4" required>-->
<!--            </div>-->
<!--            <div class="form-group">-->
<!--                <label class="form-label" for="status">是否黑名单:</label>-->
<!--                <input class="form-input" type="text" id="status" rows="4" required>-->
<!--&lt;!&ndash;            </div>&ndash;&gt;-->
<!--            <div class="form-group">-->
<!--                <label class="form-label" for="contact-number">电话号码:</label>-->
<!--                <input class="form-input" type="text" id="contact-number" rows="4" required>-->
<!--            </div>-->
            <!-- <div class="form-group">
                <label class="form-label" for="chief-complaint">主诉:</label>
                <textarea class="form-input" id="chief-complaint" rows="4" required></textarea>
            </div> -->
            <div class="form-group">
                <label class="form-label" for="medical-history">病史:</label>
                <textarea class="form-input" id="medical-history" rows="4" required></textarea>
            </div>
            <div class="form-group">
                <label class="form-label" for="diagnosis_name">确诊:</label>
                <textarea class="form-input" id="diagnosis_name" rows="4" required></textarea>
            </div>
            <div class="form-group">
                <label class="form-label">检查项目:</label>
                <div>
                    <input class="form-checkbox" type="checkbox" id="checkup1">
                    <label for="checkup1">Blood Test</label>
                </div>
                <div>
                    <input class="form-checkbox" type="checkbox" id="checkup2">
                    <label for="checkup2">X-Ray</label>
                </div>
                <div>
                    <input class="form-checkbox" type="checkbox" id="checkup3">
                    <label for="checkup3">MRI Scan</label>
                </div>
                <div>
                    <input class="form-checkbox" type="checkbox" id="checkup4">
                    <label for="checkup4">Urinalysis</label>
                </div>
                <div>
                    <input class="form-checkbox" type="checkbox" id="checkup5">
                    <label for="checkup5">ECG</label>
                </div>
                <!-- <div>
                    <input class="form-checkbox" type="checkbox" id="checkup6">
                    <label for="checkup6">肾功能</label>
                </div>
                <div>
                    <input class="form-checkbox" type="checkbox" id="checkup7">
                    <label for="checkup7">X光</label>
                </div> -->
                <!-- 其他检查项目 -->
            </div>
            <div class="form-group">
                <label class="form-label" for="prescription">处方:</label>
                <textarea class="form-input" id="prescription" rows="4" required></textarea>
            </div>
            <button class="form-button" type="submit">保存</button>
        </form>
    </div>

    <script>
        // 从URL参数中获取病人信息
        function getURLParameter(name) {
            name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]');
            var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
            var results = regex.exec(location.search);
            return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
        }

        // 更新病人信息
        function updatePatientInfo() {
            var patientId = getURLParameter('patientId');
            var patientName = getURLParameter('patientName');

            document.getElementById('patient-id').value = patientId;
            document.getElementById('patient-name').value = patientName;
        }

        // 页面加载完成后更新病人信息
        window.addEventListener('DOMContentLoaded', function () {
            updatePatientInfo();
        });

        document.getElementById('medical-form').addEventListener('submit', function (event) {
            event.preventDefault();

            // 获取表单输入值
            var medicalNumber = document.getElementById('medical-number').value;
            var visitTime = document.getElementById('visit-time').value;
            var dateOfBirth = document.getElementById('date_of_birth').value;
            var department = document.getElementById('department').value;
            var patientId = document.getElementById('patient-id').value;
            var patientName = document.getElementById('patient-name').value;
            // var age = document.getElementById('patient-age').value;
            // var cardNumber = document.getElementById('contact-number').value;
            // var chiefComplaint = document.getElementById('chief-complaint').value;
            var medicalHistory = document.getElementById('medical-history').value;
            var diagnosisName = document.getElementById('diagnosis_name').value;
            var checkup1 = document.getElementById('checkup1').checked;
            var checkup2 = document.getElementById('checkup2').checked;
            var checkup3 = document.getElementById('checkup3').checked;
            var checkup4 = document.getElementById('checkup4').checked;
            var checkup5 = document.getElementById('checkup5').checked;
            // var checkup6 = document.getElementById('checkup6').checked;
            // var checkup7 = document.getElementById('checkup7').checked;
            // var checkup8 = document.getElementById('checkup8').checked;
            // 其他检查项目

            var prescription = document.getElementById('prescription').value;

            // 构建请求体数据
            var data = {
                medicalNumber: medicalNumber,
                visitTime: visitTime,
                dateOfBirth: dateOfBirth,
                department: department,
                patientId: patientId,
                patientName: patientName,
                // gender: gender,
                // age: age,
                // cardNumber: cardNumber,
                // chiefComplaint: chiefComplaint,
                medicalHistory: medicalHistory,
                diagnosisName: diagnosisName,
                checkups: [checkup1, checkup2,checkup2,checkup3,checkup4,checkup5],  // 检查项目数组
                prescription: prescription
            };

            // 发送请求将数据保存到后端数据库
            fetch('http://localhost:8888/records/insert', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Token': window.localStorage.getItem('token')
                },
                body: JSON.stringify(data)
            }).then(function (response) {
                    if (response.ok) {
                        alert('病历保存成功');
                        // 清空表单数据
                        document.getElementById('medical-form').reset();
                    } else {
                        alert('病历保存失败，请重试');
                    }
                })
                .catch(function (error) {
                    console.error(error);
                    alert('保存出错，请稍后再试');
                });
        });
    </script>
</body>

</html>